/*
 * @brief: 
 * @Author: TzzzT
 * @Date: 2022-05-24 13:42:08
 * @LastEditTime: 2022-06-14 23:57:46
 * @LastEditors: TzzzT
 */

#include "esp_log.h"
#include "driver/gpio.h"
#include "RCS_led.h"
#include "RCS_slave.h"



static const char *LED_TAG = "LED_TAG";


void led_init(void){
    ESP_LOGI(LED_TAG, "configured GPIO LED!");
    gpio_reset_pin(GPIO_OUTPUT_PIN_SEL);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(GPIO_OUTPUT_PIN_SEL, GPIO_MODE_OUTPUT);
}

void set_led_val(bool s_led_state)
{
    /* Set the GPIO level according to the state (LOW or HIGH)*/
    gpio_set_level(GPIO_OUTPUT_PIN_SEL, s_led_state);
}

// 调用此函数获得灯接下来的状态和持续时间
//new_comein:是否从其他状态切换到此状态,  low_batt值： 0(非低电量) 1(低电量)
void waitGameStart_noConnect_state_handle(bool first_comein, uint8_t low_batt, led_t* led){
    static uint8_t num = 0;
    static bool led_state = false;
    if (first_comein)
    {
        num = 0;   
    }
    if (num < (4 + low_batt*4)) {//非低电量时，执行4次，低电量时执行8次        
        led_state = !led_state; //反转state
        if (!led_state) {//如果led_state是0，视为完成了一次亮灭,需要执行4次亮灭
            num++;
        }
        led->delay_time = 100 - 50*low_batt;
    }else if (num < (4 + low_batt*4 + LED_GAP_TIME/100)){
        //闪烁完4或8次以后，进入gap，led保持常灭LED_GAP_TIME ms。目前每100ms一次进入此线程，所以把LED_GAP_TIME
        //切割，避免一次delay1000ms以后才能重新切换到此线程，影响实时性。
        led_state = false;
        led->delay_time = 100;
        num++;
    }else   //常灭LED_GAP_TIME以后，循环回num为0
    {
        num = 0;
        led_state = true;
        led->delay_time = 100 - 50*low_batt;
    }
    led->state = led_state;
}

void scanning_state_handle(led_t* led){
    static bool led_state = true; 
    led->delay_time = 100;
    led->state = led_state;
    led_state = !led_state;
}

void waitGameStart_connect_state_handle(bool first_comein, bool low_batt, led_t* led){
    static uint8_t num = 0;
    static bool led_state = false; 
    if (!low_batt)
    {
        if (first_comein) num = 0;   //清空num，从头开始       
        if (num < 10)
        {
            led_state = true; 
            led->delay_time = 100;  
            num++;      
        }else if (num < 20)
        {
            led_state = false;
            led->delay_time = 100;
            num++;
        }else {
            num = 0;
            led_state = true; 
            led->delay_time = 10;
            //ESP_LOGI(LED_TAG, "blink once batt enough");
        }      
    }else{
        if (first_comein) num = 0;
        if (num < 16) {
            led_state = (bool)num%2;
            num++;
            led->delay_time = 50;
        }else if( num < 16 + 10){
            num++;
            led_state = false;
            led->delay_time = 100;        
        } else{
            num = 0;
            led->delay_time = 10;  
            //ESP_LOGI(LED_TAG, "blink once low batt");
        }        
    } 
    led->state = led_state; 
}

void gaming_state_handle(bool first_comein, bool team, bool living, led_t* led){
    static uint8_t dead_num = 0;
    static bool led_state = false; 
    if (living) {
        led->state = team;       
        led->delay_time = 100;
    }else{
        if (first_comein)
        {
            dead_num = 0;
        }
        if (dead_num < 4)
        {
            led_state = !led_state; 
            led->delay_time = 100; 
            if (!led_state)
            {
                dead_num++;
            }    
        }else if (dead_num < 10 + 4)
        {
            led_state = false; 
            led->delay_time = 100;
            dead_num++;
        }else {
            led_state = false; 
            led->delay_time = 10;
            dead_num = 0;
        }
        led->state = led_state;
    }   
}


//触发事件处理函数
void led_event_handle(rcs_event_t* event, led_t* led){
    static uint8_t conn_num = 0;
    static uint8_t num = 0;
    static uint8_t hit_success_num = 0;
    static bool led_state = false;
    switch (*event) {
    case CONNECT_SUCCESS:{    
        if (conn_num < 4)   //连接成功快闪4次
        {
            led_state = !led_state; 
            if (!led_state)
            {
                conn_num++;
            }
            led->delay_time = 100;        
        }else {
            conn_num = 0;
            *event = NO_EVENT;
            led_state = false; 
            led->delay_time = 10;    
        }
        led->state = led_state;   
        break;
    }
    case BE_HITTED: //目前被击中和收到遥控器指令在灯光上是一样的，区别是震动
    case REMOTE_COMM_EVENT:{//接收到遥控器指令时，灯光应该以亮灭各100ms闪烁2次，然后回到游戏中普通状态
        if (num < 2)  
        {
            led_state = !led_state; //led_state默认是false,那么反转后是true
            if (!led_state)  //如果反转了以后是false，那么执行完此次函数以后，再进入此函数时，应该是下一个闪烁了
            {
                num++;
            }
            led->delay_time = 100;
        }else if (num >= 2)  
        {
            num = 0;
            *event = NO_EVENT;
            led_state = false; 
            led->delay_time = 10;           
        }    
        led->state = led_state;  
        break;
    } 
    case HIT_SUCCESS:
        if (hit_success_num < 1)
        {
            hit_success_num++;
            led->state = true;
            led->delay_time = 50; 
        } else {
            hit_success_num = 0;
            *event = NO_EVENT;
            led->state = false; 
            led->delay_time = 10;
        }
        break;
    default:
        led->state = true;
        led->delay_time = 500;
        break;
    }    
}
